﻿using BLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;
using System.Diagnostics.Metrics;

namespace WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MenDianController : ControllerBase
    {
        public MenDianController(MenDian_BLL bll,IWebHostEnvironment env,ILogger<MenDianController> logger)
        {
            Bll = bll;
            Env = env;
            Logger = logger;
        }

        public MenDian_BLL Bll { get; }
        public IWebHostEnvironment Env { get; }
        public ILogger Logger { get; }

        /// <summary>
        /// 门店添加
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost("MDAdd")]
        public IActionResult MDAdd(MenDian_Model m)
        {
            Logger.LogInformation("门店添加成功");
            var m1= Bll.MDAdd(m);
            
            return Ok(new
            {
                data = m1
            });
        }
        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost("TP")]
        public  IActionResult TP(IFormFile file)
        {
            try
            {
                if (file.Length > 1024 * 1024 * 2)
                {
                    return Ok(new
                    {
                        data = "图片不能大于2M"
                    });
                }
                if (file.ContentType != "image/jpeg")
                {
                    return Ok(new
                    {
                        data = "图片格式不正确"
                    });
                }
                var root = Env.ContentRootPath;
                var path = root + "/img/" + file.FileName;
                using (FileStream fs = System.IO.File.Create(path))
                {
                    file.CopyTo(fs);
                    fs.Close();
                }
                return Ok(new
                {
                    data = "https://localhost:7064" + "/img/" + file.FileName
                });
            }
            catch (Exception)
            {
                return Ok(new
                {
                    data = "图片上传失败"
                });
            }
            
        }


        /// <summary>
        /// 门店列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="MDName"></param>
        /// <param name="MDZT"></param>
        /// <param name="Count"></param>
        /// <param name="Count2"></param>
        /// <returns></returns>
        [HttpGet("MDShow")]
        public IActionResult MDShow(int index, int size, string? MDName, int? MDZT)
        {
            var Count = 0;
            var Count2 = 0;
            var m=Bll.MDShow(index, size, MDName, MDZT, out Count, out Count2);
            return Ok(new
            {
                data = m,
                count = Count,
                count2 = Count2
            });
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="MDid"></param>
        /// <param name="MDZT"></param>
        /// <returns></returns>
        [HttpPost("MDXZT")]
        public IActionResult MDXZT(int MDid, int MDZT)
        {
            var m= Bll.MDXZT(MDid, MDZT);
            return Ok(new
            {
                data = m
            });
        }
        /// <summary>
        /// 门店反填
        /// </summary>
        /// <param name="MDid"></param>
        /// <returns></returns>
        [HttpPost("MDFan")]
        public IActionResult MDFan(int MDid)
        {
            var m=Bll.MDFan(MDid);
            return Ok(new
            {
                data = m
            });
        }

        /// <summary>
        /// 门店修改
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost("MDXiouGai")]
        public IActionResult MDXiouGai(MenDian_Model m)
        {

            var m1=Bll.MDXiouGai(m);
            return Ok(new
            {
                data = m1
            });
        }


    }
}
